﻿#pragma strict

/////////////////////////////////////////////////////////////////////
// 摄影机震屏器.
// 1 球形范围内随机
// 2 线性衰减
/////////////////////////////////////////////////////////////////////
var isShake:boolean;
var isUseAtten:boolean;
var shakeLastTime:float = 0.5;
var noise:Vector3 = Vector3(1,1,1);
private var shakeStopTime:float;
private var shakeStartTime:float;
var shakePos:Vector3;


private var cam:BaseCamera;
function Start () {
	cam = GetComponent.<BaseCamera>();
}

function Update () {
	Shake();
}


function Shake(){
	if(isShake){
		isShake = false;
		
		shakeStartTime = Time.time;
		shakeStopTime = Time.time + shakeLastTime;
	}
	if(Time.time < shakeStopTime){
		shakePos.Set(GetRandom(noise.x),GetRandom(noise.y),GetRandom(noise.z));
		if(isUseAtten)
			shakePos *= GetAtten();
	}else{
		shakePos = Vector3.zero;
	}
	cam.shakeDir = shakePos;
}

function GetRandom(f:float):float{
	return Random.Range(-f,f);
}

function GetAtten():float{
	return 1 - (Time.time-shakeStartTime)/shakeLastTime;
}